﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Business;
using ASBCC.Database.EntityFramework;
using System.Web.Services;
using BEGDC.Utility.Castle;
using BEGDC.CC.BL.Common;

namespace BEGDC.CC.Web
{
    public partial class cilsearch : System.Web.UI.Page
    {
        private static string jsonPattern = "*\"id\":\"{0}\",\"label\":\"{0}\",\"value\":\"{0}\"#,";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string input = Request.QueryString["term"];
                if (string.IsNullOrEmpty(input)) return;
                using (var dc = new CCEntities())
                {
                    var ems = dc.Employees.Where(em => em.NameEN.Contains(input) && em.IsEnabled == true && em.IsActived == true);
                    //var ems = dc.Employees.Where(e => e.NameEN == "Di WAN");
                    if (ems == null || ems.Count() == 0) return;
                    StringBuilder sb = new StringBuilder();
                    sb.Append("[");
                    int count = 0;
                    foreach (var em in ems)
                    {
                        if (count++ > 10) break;
                        sb.Append(string.Format(jsonPattern, em.NameEN));
                    }
                    string output = sb.ToString();
                    output = output.TrimEnd(',') + "]";
                    output = output.Replace('*', '{').Replace('#', '}');
                    Response.Clear();
                    Response.Charset = "UTF-8";
                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                    Response.ContentType = "application/json";
                    Response.Write(output);
                    Response.End();
                }
            }
            catch
            {
                return;
            }
        }


        [WebMethod]
        public static string GetRolesByNameEN(string value)
        {
            using (var dc = new CCEntities())
            {
                var em = dc.Employees.FirstOrDefault(p => p.NameEN == value);
                if (em != null)
                {
                    System.Text.StringBuilder roleids = new System.Text.StringBuilder();
                    em.Roles.Load();
                    foreach (int i in em.Roles.Where(p => !p.Disabled).Select(r => r.RoleID))
                    {
                        roleids.Append(i.ToString());
                        roleids.Append(";");
                    }
                    return roleids.ToString();
                }

            }
            return string.Empty;
        }

        [WebMethod]
        public static string GetDepartmentByCostCenterByID(int costcenterid)
        {
            BEGDC.CC.BL.Common.IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as BEGDC.CC.BL.Common.IUtility;
            return utility.GetDepartmentByCostCenter(costcenterid);
        }

        [WebMethod]
        public static string GetDepartmentByCostCenterByName(string costcentername)
        {
            BEGDC.CC.BL.Common.IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as BEGDC.CC.BL.Common.IUtility;
            return utility.GetDepartmentByCostCenter(costcentername);
        }

        [WebMethod]
        public static string GetEmployeeDetailsByNameEN(string nameen)
        {
            BEGDC.CC.BL.Common.IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as BEGDC.CC.BL.Common.IUtility;
            return utility.GetEmployeeDetailsByNameEN(nameen);
        }

        [WebMethod]
        public static string GetRMBValueFromEuroValue(double euro)
        {
            BEGDC.CC.BL.Common.IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as BEGDC.CC.BL.Common.IUtility;
            return formatestr(utility.GetRMBValueFromEuroValue(euro));
        }
        private static string formatestr(string str)
        {
            string str1 = "";
            str1 = string.Format("{0:N}", Double.Parse(str));
            str1 = string.Format("{0:F2}", str1);
            return str1;
        }
    }
}
